//
//  XCLoadingPictureViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/23.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCLoadingPictureViewController.h"

@interface XCLoadingPictureViewController ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *pageControl;
@end

@implementation XCLoadingPictureViewController
- (void)dealloc {
    _scrollView.delegate = nil;
    [_scrollView release];
    [_pageControl release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    scrollView
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, wide, high)];
    _scrollView.contentSize = CGSizeMake(wide * 4, 0);

    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    
    
    for (int i = 1; i < 5; i++) {
        UIImageView *imageme = [[UIImageView alloc] initWithFrame:CGRectMake(wide * (i - 1), 0, wide, high)];
        imageme.contentMode = UIViewContentModeScaleAspectFill;
        
        NSString *string = [NSString stringWithFormat:@"Image-loading%d",i];
        
        imageme.image = [UIImage imageNamed:string];
        
        
        [_scrollView addSubview:imageme];
        [imageme release];
        if (i == 4) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(wide / 3, high * 3 / 4, wide / 3, high * 0.06);
            button.layer.cornerRadius = 10;
            button.layer.borderWidth = 1;
            
            [button setTitle:@"欢迎体验" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
            [button addTarget:self action:@selector(closeTheVC) forControlEvents:UIControlEventTouchUpInside];
            imageme.userInteractionEnabled = YES;
            [imageme addSubview:button];
            
        }
    }

    [self.view addSubview:_scrollView];
    [_scrollView release];
    _scrollView.showsHorizontalScrollIndicator = NO;
//    pageControll
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, high - 35,  wide, 20)];
    self.pageControl.numberOfPages = 4;
    [self.pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.pageControl];
    [_pageControl release];
    
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    
    return [scrollView.subviews firstObject];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
      _pageControl.currentPage = _scrollView.contentOffset.x/_scrollView.bounds.size.width;
}


- (void)pageControlAction:(UIPageControl *) pageControl {
    [self.scrollView setContentOffset:CGPointMake(wide * pageControl.currentPage, 0) animated:YES];
    
}
- (void)closeTheVC {
    [self.delegate sender];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
